package xyz.mumu233.robot.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import xyz.mumu233.robot.bean.api.xiaoi.XiaoIResult;

import java.io.IOException;

/**
 * @Author mumu
 * @create 2019/5/7 11:55
 */
@Slf4j
public class XiaoI {

    public static XiaoIResult getReply(String question){

        OkHttpClient client = new OkHttpClient();

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "question="+question+"&appkey=f099757f5bd76174");
        Request request = new Request.Builder()
                .url("https://api.jisuapi.com/iqa/query")
                .post(body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("User-Agent", "PostmanRuntime/7.11.0")
                .addHeader("Accept", "*/*")
                .addHeader("Cache-Control", "no-cache")
                .addHeader("Postman-Token", "226b633e-400a-4129-a096-72d427377901,b760e82a-5dd3-4e94-acaa-481904d40c54")
                .addHeader("Host", "api.jisuapi.com")
//                .addHeader("accept-encoding", "gzip, deflate")
                .addHeader("content-length", "60")
                .addHeader("Connection", "keep-alive")
                .addHeader("cache-control", "no-cache")
                .build();
        try{
            Response response = client.newCall(request).execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            XiaoIResult xiaoIResult = JSON.toJavaObject(jsonObject,XiaoIResult.class);
            return xiaoIResult;
        }catch (IOException e){
            log.debug("获取智能回复出现错误");
        }

     return null;
    }
}
